package com.platform.server.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.platform.server.dao.AppealComplaintDao;
import com.platform.server.dao.MessageDao;
import com.platform.server.dao.UserDao;
import com.platform.server.entity.AppealComplaint;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.service.AppealComplaintService;
import com.platform.server.util.StringTools;

@Transactional
@Service
public class AppealComplaintServiceImpl implements AppealComplaintService{

    @Autowired
    private AppealComplaintDao appealComplaintDao;
    @Autowired
    private MessageDao messageDao;
    @Autowired
    private UserDao userDao;
    /**
     * 申诉/投诉 
     */
	@Override
	public ReturnData addAppealComplaint(AppealComplaint AppealComplaint) {
		 if (StringTools.isEmpty(AppealComplaint.getContent()) || StringTools.isEmpty(AppealComplaint.getAppeal_complaint_user()) ||
	                StringTools.isEmpty(AppealComplaint.getUser_id())  || StringTools.isEmpty(AppealComplaint.getOrder_id())  || 
	                StringTools.isEmpty(AppealComplaint.getIs_status())) {
	            return new ReturnData(12, "参数有误");
	        }
			if(AppealComplaint.getIs_status().equals("0") && StringTools.isEmpty(AppealComplaint.getPicture())){
		 		 return new ReturnData(12, "参数有误");
		 	}
			//查询是否申诉/投诉
			Map<String,Object> map= appealComplaintDao.selectAppealComplaint(AppealComplaint);
		 if(map!=null){
			 return new ReturnData(12, AppealComplaint.getIs_status().equals("0") ? "投诉已经申请":"申诉已经申请");
		 }
		   int flag = appealComplaintDao.addAppealComplaint(AppealComplaint);
		   if(!StringTools.isEmpty(AppealComplaint.getPicture())){
			   map= appealComplaintDao.selectAppealComplaint(AppealComplaint);
			   AppealComplaint.setAppeal_complaint_id(map.get("id").toString());
			   String[] split = AppealComplaint.getPicture().split(",");
			   for (int i = 0; i < split.length; i++) {
				   AppealComplaint.setPicture(split[i]);
				   int addAppealComplaintPicture = appealComplaintDao.addAppealComplaintPicture(AppealComplaint);
				   if (addAppealComplaintPicture != 1) {
					   return new ReturnData(100, AppealComplaint.getIs_status().equals("0") ? "投诉失败":"申诉失败");
				   }
			}
		   }
	        if (flag == 1) {
	        	Map<String, Object> selectUserMobile = userDao.selectUserMobile(AppealComplaint.getAppeal_complaint_user());
	        	if( AppealComplaint.getIs_status().equals("0")){
	        		 Map<String, Object> hm = new HashMap<>();
			         hm.put("title", selectUserMobile.get("username").toString()+"投诉了您，请前去我的投诉/申诉查看  ");
			         hm.put("type", "1");
			         hm.put("recevicer_id", selectUserMobile.get("id").toString());
			         hm.put("content", selectUserMobile.get("username").toString()+"投诉了您，请前去我的投诉/申诉查看  ");
			         messageDao.insertMessage(hm);
	        	}else{
	        		 Map<String, Object> hm = new HashMap<>();
			         hm.put("title", selectUserMobile.get("username").toString()+"对您的投诉进行了申诉，请前去我的投诉/申诉查看");
			         hm.put("type", "1");
			         hm.put("recevicer_id", selectUserMobile.get("id").toString());
			         hm.put("content", selectUserMobile.get("username").toString()+"对您的投诉进行了申诉，请前去我的投诉/申诉查看 ");
			         messageDao.insertMessage(hm);
	        	}
	            return new ReturnData(200, AppealComplaint.getIs_status().equals("0") ? "投诉成功":"申诉成功");
	        }
	        return new ReturnData(100, AppealComplaint.getIs_status().equals("0") ? "投诉失败":"申诉失败");
	}
	 /**
     * 
     * @param 查询投诉/申诉订单详情
     * @return
     */
	@Override
	public ReturnData selectAppealComplaintDetail(
			AppealComplaint appealComplaint) {
		 if (StringTools.isEmpty(appealComplaint.getUser_id()) || StringTools.isEmpty(appealComplaint.getOrder_id())) {
	            return new ReturnData(12, "参数有误");
	        }
		 Map<String, Object> selectAppealComplaintDetail = appealComplaintDao.selectAppealComplaintDetail(appealComplaint);
		 if(selectAppealComplaintDetail.get("tousuid")!=null){
			 List<Map<String,Object>> selectAppealComplaintPic = appealComplaintDao.selectAppealComplaintPic(selectAppealComplaintDetail.get("tousuid").toString());
			 selectAppealComplaintDetail.put("tousupic", selectAppealComplaintPic);
		 }
		 if(selectAppealComplaintDetail.get("shensuid")!=null){
		 List<Map<String,Object>> selectAppealComplaintPic2 = appealComplaintDao.selectAppealComplaintPic(selectAppealComplaintDetail.get("shensuid").toString());
		 selectAppealComplaintDetail.put("shensupic", selectAppealComplaintPic2);
		 }
		 if(selectAppealComplaintDetail!=null &&selectAppealComplaintDetail.size()>0){
			 return new ReturnData(selectAppealComplaintDetail,200, "");
		 }
		   return new ReturnData(100, "暂无数据");
	}
	
	  /**
     * 
     * @param 查询投诉/申诉订单列表
     * @return
     */
	@Override
	public ReturnData selectAppealComplaintList(AppealComplaint appealComplaint) {
		 if (StringTools.isEmpty(appealComplaint.getUser_id()) || StringTools.isEmpty(appealComplaint.getIs_status())) {
	            return new ReturnData(12, "参数有误");
	        }
		 String a=appealComplaint.getIs_status();
		 if(appealComplaint.getIs_status().equals("1")){
			 appealComplaint.setIs_status("0");
			 appealComplaint.setAppeal_complaint_user(appealComplaint.getUser_id());
			 appealComplaint.setUser_id(null);
		 }
		 List<Map<String,Object>> selectAppealComplaintList = appealComplaintDao.selectAppealComplaintList(appealComplaint);
		 if(selectAppealComplaintList.size()==0&&a.equals("1")){
			 appealComplaint.setAppeal_complaint_id(appealComplaint.getAppeal_complaint_user());
			 appealComplaint.setAppeal_complaint_user(null);
			  selectAppealComplaintList = appealComplaintDao.selectAppealComplaintList(appealComplaint);
		 }
		 if(selectAppealComplaintList!=null&&selectAppealComplaintList.size()>0){
			 return new ReturnData(selectAppealComplaintList,200, "");
		 }
		  return new ReturnData(100, "暂无数据");
	}
}
